/*    
 * Copyright (c) 2013 ISoftStone, Inc.
 * All right reserved.
 *
 * 文件名：      ProductInstServiceImpl.java
 * 作者:     Jacky Wang
 * 创建日期： 2013-8-1 上午10:41:16
 * 版本：           
 *
 */
package com.isoftstone.industry.aaa.core.service.assist.impl;

import java.sql.Timestamp;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.isoftstone.industry.common.util.SNGenerator;
import com.isoftstone.industry.constant.AppConstant;
import com.isoftstone.industry.constant.IConstants;
import com.isoftstone.industry.persistance.bean.AppInstance;
import com.isoftstone.industry.persistance.bean.AppWorkOrder;
import com.isoftstone.industry.persistance.bean.Application;
import com.isoftstone.industry.persistance.bean.ProductInstance;
import com.isoftstone.industry.aaa.core.dao.assist.AppInstanceDao;
import com.isoftstone.industry.aaa.core.dao.assist.AppWorkOrderDao;
import com.isoftstone.industry.aaa.core.dao.assist.ProductInstDao;
import com.isoftstone.industry.aaa.core.service.assist.IProductInstService;
import com.isoftstone.modules.core.persistence.IGenericDao;
import com.isoftstone.modules.core.service.impl.GenericServiceImpl;
import com.isoftstone.modules.core.utils.DateUtils;

/**
 * @author jacky
 */
@Service
public class ProductInstServiceImpl extends GenericServiceImpl<ProductInstance, String> implements IProductInstService
{

	@Resource
	private ProductInstDao	prodInstDao;
	
	@Resource
	private AppInstanceDao appInstDao;
	
	@Resource
	private AppWorkOrderDao workOrderDao;

	@Override
	public IGenericDao<ProductInstance, String> getDao()
	{
		return prodInstDao;
	}

	@Override
	@Transactional
	public void disable(String prodInstId)
	{
		ProductInstance prodInst = prodInstDao.findOne(prodInstId);
		
		if (ProductInstance.IStatus.ENABLED.equals(prodInst.getStatus()))
		{
			prodInstDao.changeStatus(prodInstId, ProductInstance.IStatus.DISABLED);

			AppInstance appInst = prodInst.getAppInstance();
			Timestamp now = DateUtils.getCurrentTimestamp();
			appInstDao.changeStatus(appInst.getId(), AppInstance.Status.DISABLED, now);

			Application app = appInst.getApplication();
			String type = null;
			if (app.getType().equals(Application.APP_TYPE.APP_TYPE_1))
			{
				type = IConstants.OPTYPE_SAAS_BIZZE_SUSPEND;
			} else if (app.getType().equals(Application.APP_TYPE.APP_TYPE_3))
			{
				type = IConstants.OPTYPE_VM_BIZZ_SUSPEND;
			} else if (app.getType().equals(Application.APP_TYPE.APP_TYPE_4))
			{
				type = IConstants.OPTYPE_VM_BIZZ_SUSPEND_INNER;
			}

			AppWorkOrder appWorkOrder = new AppWorkOrder();
			appWorkOrder.setAppInstance(appInst);
			appWorkOrder.setSn(SNGenerator.createAppWorkOrderSN());

			appWorkOrder.setType(type);
			appWorkOrder.setStatus(AppWorkOrder.Status.APP_WORK_ORDER_STATUS_0); //未处理
			appWorkOrder.setCreatedTime(now);
			appWorkOrder.setStatusTime(now);
			appWorkOrder.setDeleted(false);
			appWorkOrder.setWorker("SYSTEM");

			workOrderDao.save(appWorkOrder);
		}
	}

	@Override
	@Transactional
	public void enable(String prodInstId)
	{
		ProductInstance prodInst = prodInstDao.findOne(prodInstId);

		if (ProductInstance.IStatus.DISABLED.equals(prodInst.getStatus()))
		{
			prodInstDao.changeStatus(prodInstId, ProductInstance.IStatus.ENABLED);

			AppInstance appInst = prodInst.getAppInstance();
			Timestamp now = DateUtils.getCurrentTimestamp();
			appInstDao.changeStatus(appInst.getId(), AppInstance.Status.ENABLED, now);

			Application app = appInst.getApplication();
			String type = null;
			if (app.getType().equals(Application.APP_TYPE.APP_TYPE_1))
			{
				type = IConstants.OPTYPE_SAAS_BIZZE_RECOVE;
			} else if (app.getType().equals(Application.APP_TYPE.APP_TYPE_3))
			{
				type = IConstants.OPTYPE_VM_BIZZ_RECOVE;
			} else if (app.getType().equals(Application.APP_TYPE.APP_TYPE_4))
			{
				type = IConstants.OPTYPE_VM_BIZZ_RECOVE_INNER;
			}

			AppWorkOrder appWorkOrder = new AppWorkOrder();
			appWorkOrder.setAppInstance(appInst);
			appWorkOrder.setSn(SNGenerator.createAppWorkOrderSN());

			appWorkOrder.setType(type);
			appWorkOrder.setStatus(AppWorkOrder.Status.APP_WORK_ORDER_STATUS_0); //未处理
			appWorkOrder.setCreatedTime(now);
			appWorkOrder.setStatusTime(now);
			appWorkOrder.setDeleted(false);
			appWorkOrder.setWorker("SYSTEM");

			workOrderDao.save(appWorkOrder);
		}
	}

}
